/**
 * 依赖全局变量 InfoGlobal
 * Created by shopnc on 2015/11/26.
 */
//定义表格
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'title',
        title: '标题',
        type: 'string',
        headerClass: 'text-left width-200',
        columnClass: 'text-left width-200',
        fastQuery: true,
        fastQueryType: 'lk',
        fastSort: false,
        hideType: 'sm|xs'
    },
    {
        id: 'releaseTime',
        title: '发布时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-left width-200',
        columnClass: 'text-left width-200',
        fastQuery: true,
        fastQueryType: 'range',
        hideType: 'md|sm|xs'
    },
    {
        id: 'status',
        title: '状态',
        type: 'string',
        columnClass: 'text-center width-150',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        hideType: 'md|sm|xs',
        codeTable: {
            1: $lang.articleStatus.WAIT,
            2: $lang.articleStatus.PUBLISHED,
            3: $lang.articleStatus.BACK
        }
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        columnClass: 'text-center width-400',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html = "",
                edit = "",
                publish = "",
                back = "",
                del = "";
            if ($("#announceEdit") && $("#announceEdit").val() == 1) {
                edit = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            }

            if ($("#announcePublish") && $("#announcePublish").val() == 1) {
                publish = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='information.pubInfo(" + record.id + ",\"" + record.title + "\")'><i class='fa fa-paper-plane-o'></i>&nbsp;发布&nbsp;</a>";
            }

            if ($("#announceBack") && $("#announceBack").val() == 1) {
                back = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='information.backInfo(" + record.id + ",\"" + record.title + "\")'><i class='fa fa-reply-all'></i>&nbsp;撤回&nbsp;</a>";
            }

            if ($("#announceDel") && $("#announceDel").val() == 1) {
                del = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='information.delInfo(" + record.id + ",\"" + record.title + "\")'><i class='fa fa-trash-o'></i>&nbsp;删除&nbsp;</a>";
            }
            if (record.status != 2) {
                html = edit + publish + del;
            } else if (record.status == 2) {
                html = back ;
            }
            return html;
        }
    }
];
var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'announce/json/list',
    exportFileName: '公告列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id","status","delFlag"], Timestamp: ["createTime","releaseTime"]},
    onGridComplete: function (grid) {
    }
};
var grid = $.fn.DtGrid.init(dtGridOption);
grid.sortParameter.columnId = ['asc_id'];

grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
grid.sortParameter.columnId = ['desc_releaseTime','asc_id'];
/**
 * 公告列表所使用的js
 */
var information = function () {

    /**
     * 刷新addmodal 上的图片
     */


        //编辑器对象[添加]
    var _ueAdd;
    //编辑器对象[编辑]
    var _ueEdit;
    //删除URL
    var delUrl = ncGlobal.adminRoot + "announce/json/del";
    //撤回URL
    var backUrl = ncGlobal.adminRoot + "announce/json/back";
    //发布URL
    var publishUrl = ncGlobal.adminRoot + "announce/json/publish";

    /**
     * 删除
     */
    function _delInfo(id, content) {
        var tpl = '您选择对公告 <strong>' + content + '</strong> 进行删除操作，删除后将无法恢复。<br/>您确定要进行删除操作吗?'
        $.ncConfirm({
            url: delUrl,
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "删除操作"
        });
    }

    /**
     * 撤回
     */
    function _backInfo(id, content) {
        var tpl = '您是否确定撤回公告 <strong>' + content + '</strong> ?'
        $.ncConfirm({
            url: backUrl,
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "撤回操作"
        });
    }

    /**
     * 发布
     */
    function _pubInfo(id, content) {
        var tpl = '您是否确定发布公告 <strong>' + content + '</strong> ?'
        $.ncConfirm({
            url: publishUrl,
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "发布操作"
        });
    }

    var addInfo = {
        initAddModal: function () {
            $addForm = $("#addForm");
            $(".alert-danger").remove();
            $addForm.psly().reset();
            $addForm.find('input[name="title"]').val("");
            $("#contentAdd").val("");
        }
    }

    /**
     * 事件绑定
     * @private
     */
    function _bindEvent() {
        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters['lk_title'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });


        // //图片上传
        // $("#addFormPic").fileupload({
        //     dataType: 'json',
        //     url: ncGlobal.adminRoot + "file/upload",
        //     send: function (e, data) {
        //         //进行图片格式验证
        //         var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
        //         if(reg.test(data.files[0].type)){
        //             if(data.files[0].size>2*1024*1024){
        //                 $.ncAlert({
        //                     closeButtonText: "关闭",
        //                     autoCloseTime: 3,
        //                     content: "图片格式超过2M请重新上传！"
        //                 });
        //             }
        //         }else{
        //             $.ncAlert({
        //                 closeButtonText: "关闭",
        //                 autoCloseTime: 3,
        //                 content: "请上传符合格式要求的图片！"
        //             });
        //             return;
        //         }
        //     },
        //     done: function (e, data) {
        //         if (data.result.code == 200) {
        //             $('#addFormPicPath').val(data.result.data);
        //             $('#addFormPicImg').attr('src', data.result.data);
        //             //图片同比例缩放-新增
        //             _refreshImage();
        //         } else {
        //             $.ncAlert({
        //                 closeButtonText: "关闭",
        //                 autoCloseTime: 3,
        //                 content: data.result.message
        //             })
        //         }
        //     }
        // });
        // /**
        //  * 上传插件绑定
        //  */
        // $("#editFormPic").fileupload({
        //     dataType: 'json',
        //     url: ncGlobal.adminRoot + "file/upload",
        //     send: function (e, data) {
        //         //进行图片格式验证
        //         var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
        //         if(reg.test(data.files[0].type)){
        //             if(data.files[0].size>2*1024*1024){
        //                 $.ncAlert({
        //                     closeButtonText: "关闭",
        //                     autoCloseTime: 3,
        //                     content: "图片格式超过2M请重新上传！"
        //                 });
        //             }
        //         }else{
        //             $.ncAlert({
        //                 closeButtonText: "关闭",
        //                 autoCloseTime: 3,
        //                 content: "请上传符合格式要求的图片！"
        //             });
        //             return;
        //         }
        //     },
        //     send: function (e, data) {
        //         //进行图片格式验证
        //         var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
        //         if(reg.test(data.files[0].type)){
        //             if(data.files[0].size>2*1024*1024){
        //                 $.ncAlert({
        //                     closeButtonText: "关闭",
        //                     autoCloseTime: 3,
        //                     content: "图片格式超过2M请重新上传！"
        //                 });
        //             }
        //         }else{
        //             $.ncAlert({
        //                 closeButtonText: "关闭",
        //                 autoCloseTime: 3,
        //                 content: "请上传符合格式要求的图片！"
        //             });
        //             return;
        //         }
        //     },
        //     done: function (e, data) {
        //         if (data.result.code == 200) {
        //             $('#editFormPicPath').val(data.result.data);
        //             $('#editFormPicImg').attr('src', data.result.data);
        //             //图片同比例缩放-编辑
        //             _refreshImage();
        //         } else {
        //             $.ncAlert({
        //                 closeButtonText: "关闭",
        //                 autoCloseTime: 3,
        //                 content: data.result.message
        //             })
        //         }
        //     }
        // });

        // 新增公告对话框
        $("#addForm").on("nc.formSubmit.success", function (e) {
            addInfo.initAddModal();
        })

        //添加微信事件时显示时
        $('#addModal').on('show.bs.modal', function(event) {
            addInfo.initAddModal();
        });

        // bycj [ 编辑对话框显示时调用 ]
        $('#editModal').on('show.bs.modal', function (event) {
            var    //获取接受事件的元素
                button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),
                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano],
                editForm = $("#editForm");
            editForm.find('input[name="id"]').val(gridData.id);
            editForm.find('input[name="title"]').val(gridData.title);
            $('#contentEdit').val(gridData.content);
            //清除错误提示
            editForm.psly().reset();
            $(".alert-danger").remove()
        });


    }

    //外部可调用
    return {
        init: function () {
            _bindEvent();
        },
        delInfo: _delInfo,
        backInfo: _backInfo,
        pubInfo: _pubInfo
    }
}()
$(function () {
    grid.load();
    information.init();
})
